/**
 * 页面的初始数据
 */
Page({
  data: {
    numberRange: [],
    multiRange: [[], []],
    selectedNumberIndex: 0,
    selectedMultiIndex: [0, 0],
    minValue: 1,
    maxValue: 10,
  },
  onLoad: function () {
    const numberRange = [];
    for (let i = 1; i <= 50; i++) {
      numberRange.push(i.toString());
    }
  
    const multiRange = [];
    const subMultiRange = [];
    for (let i = 1; i <= 50; i++) {
      multiRange.push(i.toString());
      subMultiRange.push((i + 1).toString());
    }
  
    this.setData({
      numberRange: numberRange,
      multiRange: [multiRange, subMultiRange],
    });
  },
  bindNumberChange: function (e) {
    this.setData({
      selectedNumberIndex: e.detail.value
    })
  },
  bindMultiChange: function (e) {
    this.setData({
      selectedMultiIndex: e.detail.value
    })
  },
  bindSliderChange: function (e) {
    this.setData({
      minValue: e.detail.value[0],
      maxValue: e.detail.value[1]
    })
  },
  startExtraction: function () {
    // 获取用户选择的抽取个数、抽取范围等信息
    const selectedNumberIndex = this.data.selectedNumberIndex;
    const selectedMultiIndex = this.data.selectedMultiIndex;
    const minValue = this.data.minValue;
    const maxValue = this.data.maxValue;
    
    // 进行抽取操作
    const extractedNumbers = [];
    const extractCount = parseInt(this.data.numberRange[selectedNumberIndex]);
    const startRange = parseInt(this.data.multiRange[0][selectedMultiIndex[0]]);
    const endRange = parseInt(this.data.multiRange[1][selectedMultiIndex[1]]);

    const availableNumbers = [];

    // 生成可选数字的数组
    for (let i = startRange; i <= endRange; i++) {
      availableNumbers.push(i);
    }

    // 检查可选数字数量是否足够满足抽取个数
    if (extractCount > availableNumbers.length) {
      wx.showModal({
        title: '提示',
        content: '可选数字的数量不足，无法完成抽取。',
        showCancel: false,
      });
      return;
    }

    // 随机抽取数字，并移除已抽取的数字
    for (let i = 0; i < extractCount; i++) {
      const randomIndex = Math.floor(Math.random() * availableNumbers.length);
      const extractedNumber = availableNumbers[randomIndex];
      extractedNumbers.push(extractedNumber);
      availableNumbers.splice(randomIndex, 1);
    }

    // 弹窗显示抽取结果
    wx.showModal({
      title: '抽取结果',
      content: extractedNumbers.join(','),
      showCancel: false,
    })
  },
})